package com.source.lightfowl.authorization;

import com.source.lightfowl.constant.SessionKey;
import com.source.lightfowl.person.entity.Plate;
import com.source.lightfowl.person.entity.PlateExtends;
import com.source.lightfowl.person.entity.UsersExtends;
import com.source.lightfowl.syslog.aspect.SysLogAspect;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.StatusCode;
//import jdk.nashorn.internal.runtime.OptimisticBuiltins;
import org.apache.zookeeper.Op;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
//import org.omg.CORBA.UserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Description: 检查接口访问权限(access)
 * @Author: Faink
 * @CreateTime: 2020/11/1 16:48
 */
@Aspect
@Component
public class CheckPermissionsAspect{
    private static final Logger log = LoggerFactory.getLogger(CheckPermissionsAspect.class);

    @Pointcut("@annotation( com.source.lightfowl.authorization.CheckPermissions)")
    public void checkPermissions(){}
/*
    @Around("checkPermissions()")
    public Object checkPermiss(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("============= 检查权限 =============");
        Boolean accessAuth = false;
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        CheckPermissions annotation = method.getAnnotation(CheckPermissions.class);
        String value = annotation.value();
        String[] splitStr = value.split(":");
        if (splitStr.length == 3) {
            ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
            HttpSession session = attributes.getRequest().getSession();
            Object attribute = session.getAttribute(SessionKey.USERSEXTENDS_SESSION_KEY);
            if (null == attribute) {
                log.info("============= 权限不足 =============");
                return MyResult.build(StatusCode.FORBIDDEN, "权限不足");
            }
            UsersExtends usersExtends = (UsersExtends)attribute;
            List<PlateExtends> plateExtendsList =
                    usersExtends.getPlateExtendsList() == null ? null : usersExtends.getPlateExtendsList();
            if (null != plateExtendsList && plateExtendsList.size()!=0) {
                for (PlateExtends plateExtends : plateExtendsList) {
                    String[] eids = plateExtends.getEids();
                    if (null != eids && eids.length > 0) {
                        Plate plate = plateExtends.getPlates();
                        String bigpid = plate.getBigpid();
                        String pid = plate.getPid();
                        if (bigpid.equals(splitStr[0]) && pid.equals(splitStr[1])) {
                            for (String eid : eids) {
                                if (eid.equals(splitStr[2])) {
                                    accessAuth = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (accessAuth) {
                        log.info("============= 权限通过 =============");
                        return joinPoint.proceed();
                    }
                }
            }
        }
        else {
            log.info("============= 格式错误 =============");
            return MyResult.build(StatusCode.ERROR, "注解格式错误");
        }
        log.info("============= 权限不足 =============");
        return MyResult.build(StatusCode.FORBIDDEN, "权限不足");
    }*/
}
